//
//  PredictRecordCell.m
//  WanCaiC8
//
//  Created by tangang on 2018/7/19.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "PredictRecordCell.h"

@implementation PredictRecordCell{
    UIView      *bgView;
    QXLable     *Issue;
    QXLable     *openNum;
    QXLable     *predictNum;
    QXLable     *results;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        bgView =[[UIView alloc]init];
//        bgView.layer.masksToBounds=YES;
//        bgView.layer.borderWidth=.7;
//        bgView.layer.borderColor=DEFAULTBGCOLOR.CGColor;
        [self.contentView addSubview:bgView];
        [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).with.offset(10);
            make.top.bottom.equalTo(self.contentView);
            make.right.equalTo(self.contentView).with.offset(-10);
        }];
        
        UIView *line3=[[UIView alloc]init];
        line3.backgroundColor=DEFAULTBGCOLOR;
        [bgView addSubview:line3];
        [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.left.bottom.equalTo(bgView);
            make.height.mas_equalTo(@.7);
        }];
        
        UIView *line4=[[UIView alloc]init];
        line4.backgroundColor=DEFAULTBGCOLOR;
        [bgView addSubview:line4];
        [line4 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.equalTo(bgView);
            make.width.mas_equalTo(@.7);
        }];
        
        
        UIView *line5=[[UIView alloc]init];
        line5.backgroundColor=DEFAULTBGCOLOR;
        [bgView addSubview:line5];
        [line5 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.bottom.equalTo(bgView);
            make.width.mas_equalTo(@.7);
        }];
        
        
        Issue=[[QXLable alloc]init];
        Issue.textAlignment=NSTextAlignmentCenter;
        [bgView addSubview:Issue];
        [Issue mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.equalTo(bgView);
            make.width.mas_equalTo(@(real_size(70)));
        }];
        UIView *line=[[UIView alloc]init];
        line.backgroundColor=DEFAULTBGCOLOR;
        [self.contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.bottom.equalTo(Issue);
            make.width.mas_equalTo(@.7);
        }];
        
        openNum=[[QXLable alloc]init];
        openNum.textAlignment=NSTextAlignmentCenter;
        [bgView addSubview:openNum];
        [openNum mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(bgView);
            make.left.equalTo(Issue.mas_right);
            make.width.mas_equalTo(@(real_size(115)));
        }];
        
        UIView *line1=[[UIView alloc]init];
        line1.backgroundColor=DEFAULTBGCOLOR;
        [bgView addSubview:line1];
        [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.bottom.equalTo(openNum);
            make.width.mas_equalTo(@.7);
        }];
        
        predictNum=[[QXLable alloc]init];
        predictNum.textAlignment=NSTextAlignmentCenter;
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onclickLookNew)];
        [predictNum addGestureRecognizer:tap];
        [bgView addSubview:predictNum];
        [predictNum mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(bgView).with.offset(15);
            make.bottom.equalTo(bgView).with.offset(-15);
            make.left.equalTo(openNum.mas_right);
            make.width.mas_equalTo(@(real_size(115)));
        }];
        
        UIView *line2=[[UIView alloc]init];
        line2.backgroundColor=DEFAULTBGCOLOR;
        [bgView addSubview:line2];
        [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(predictNum);
            make.top.bottom.equalTo(bgView);
            make.width.mas_equalTo(@.7);
        }];
        
        results=[[QXLable alloc]init];
        results.textAlignment=NSTextAlignmentCenter;
        [bgView addSubview:results];
        [results mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.equalTo(bgView);
            make.left.equalTo(predictNum.mas_right);
        }];
        

    }
    return self;
}

- (void)onclickLookNew{
    if (self.lookNew) {
        self.lookNew();
    }
}

- (void)setModel:(BetModel *)model andRow:(NSInteger)row{
    predictNum.userInteractionEnabled=NO;
    openNum.attributedText=nil;
    predictNum.attributedText=nil;
    if (row==0) {
        Issue.text=@"期号";
        Issue.font=[UIFont systemFontOfSize:15];
        Issue.textColor=[UIColor whiteColor];
        openNum.text=@"开奖";
        openNum.font=[UIFont systemFontOfSize:15];
        openNum.textColor=[UIColor whiteColor];
        predictNum.text=@"预测内容";
        predictNum.font=[UIFont systemFontOfSize:15];
        predictNum.textColor=[UIColor whiteColor];
        results.text=@"结果";
        results.font=[UIFont systemFontOfSize:15];
        results.textColor=[UIColor whiteColor];
        bgView.backgroundColor=NAVCOLOR;
    }else if (row==1){
        Issue.text=model.Issue;
        Issue.font=[UIFont systemFontOfSize:13];
        Issue.textColor=DEFAULTTEXTBLACKCOLOR;
        openNum.text=@"未开奖";
        openNum.font=[UIFont systemFontOfSize:13];
        openNum.textColor=DEFAULTTEXTBLACKCOLOR;
        if (model.BetNum.length>0) {
            predictNum.text=model.BetNum;
            predictNum.textColor=DEFAULTTEXTBLACKCOLOR;
            predictNum.font=[UIFont systemFontOfSize:13];
        }else{
            predictNum.userInteractionEnabled=YES;
            predictNum.text=@"点击查看";
            predictNum.textColor=NAVCOLOR;
            predictNum.font=[UIFont systemFontOfSize:13];
        }
        
        results.text=@"?";
        results.font=[UIFont systemFontOfSize:13];
        results.textColor=DEFAULTTEXTBLACKCOLOR;
        bgView.backgroundColor=[UIColor colorWithHexString:@"f13131" alpha:.4];
    }else{
        Issue.text=model.Issue;
        Issue.font=[UIFont systemFontOfSize:13];
        Issue.textColor=DEFAULTTEXTBLACKCOLOR;
        
        openNum.font=[UIFont systemFontOfSize:13];
        openNum.textColor=DEFAULTTEXTBLACKCOLOR;
        
        predictNum.font=[UIFont systemFontOfSize:13];
        predictNum.textColor=DEFAULTTEXTBLACKCOLOR;
        
        if ([model.WinState intValue] == 3) {
            results.text=@"中";
            results.textColor=NAVCOLOR;
            openNum.attributedText=[self attStrWithOpenNum:model];
            predictNum.attributedText=[self attStrWithBetNum:model];
        }else if([model.WinState intValue] == 1){
            results.text=@"兑奖中";
            results.textColor=DEFAULTTEXTGRAYCOLOR;
            openNum.text=model.CorrectNum;
            predictNum.text=model.BetNum;
        }else{
            results.text=@"错";
            results.textColor=DEFAULTTEXTBLACKCOLOR;
            openNum.text=model.CorrectNum;
            predictNum.text=model.BetNum;
        }
        
        results.font=[UIFont systemFontOfSize:13];
        
        bgView.backgroundColor=[UIColor whiteColor];
    }
}

//待修改
- (NSMutableAttributedString *)attStrWithBetNum:(BetModel *)model{
    NSMutableAttributedString *attStr=[[NSMutableAttributedString alloc]initWithString:model.BetNum];
    if (attStr.length==1) {
        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:NSMakeRange(0, 1)];
        return attStr;
    }
    NSArray *arrNum=[model.BetNum componentsSeparatedByString:@","];
    NSArray *openArr=[model.CorrectNum componentsSeparatedByString:@","];
    LotteryNumType type=[[BaseVM manage] getType:model.lType];
    if (type==LotteryNumTypeCarPK10||type==LotteryNumTypeLuckyAirship){
        NSInteger index=0;
        if ([model.PlayName isEqualToString:@"冠军五码"]) {
            index=0;
        }else if ([model.PlayName isEqualToString:@"亚军五码"]){
            index=1;
        }else if ([model.PlayName isEqualToString:@"第三名五码"]){
            index=2;
        }else if ([model.PlayName isEqualToString:@"第四名五码"]){
            index=3;
        }else if ([model.PlayName isEqualToString:@"第五名五码"]){
            index=4;
        }else if ([model.PlayName isEqualToString:@"第六名五码"]){
            index=5;
        }else if ([model.PlayName isEqualToString:@"第七名五码"]){
            index=6;
        }else if ([model.PlayName isEqualToString:@"第八名五码"]){
            index=7;
        }else if ([model.PlayName isEqualToString:@"第九名五码"]){
            index=8;
        }else if ([model.PlayName isEqualToString:@"第十名五码"]){
            index=9;
        }
        if (openArr.count>index) {
            NSString *str=openArr[index];
            for (NSString *strNum in arrNum) {
                if ([str isEqualToString:strNum]) {
                    NSRange rang=[model.BetNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    break;
                }
            }
        }
        
    }else if (type==LotteryNumTypeBrings) {
        if ([model.PlayName isEqualToString:@"前区4码"]||[model.PlayName isEqualToString:@"前区18码"]) {
            for (NSString *str in arrNum) {
                for (int i=0;i<openArr.count-2;i++) {
                    NSString *strNum=openArr[i];
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.PlayName isEqualToString:@"后区3码"]||[model.PlayName isEqualToString:@"后区6码"]){
            if (openArr.count!=7) {
                return attStr;
            }
            for (NSString *str in arrNum) {
                for (int i=5;i<openArr.count;i++) {
                    NSString *strNum=openArr[i];
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }
    }else if (type==LotteryNumTypeLottery3D||type==LotteryNumType11Select5||type==LotteryNumTypeArrangement3||type==LotteryNumTypeArrangement5||type==LotteryNumTypeSevenLecai||type==LotteryNumTypeSevenStars||type==LotteryNumTypeAllTime) {
        if ([model.PlayName isEqualToString:@"第一球五码"]) {
            if (openArr.count>0) {
                NSString *str=openArr[0];
                for (NSString *strNum in arrNum) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.PlayName isEqualToString:@"第二球五码"]){
            if (openArr.count>1) {
                NSString *str=openArr[1];
                for (NSString *strNum in arrNum) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.PlayName isEqualToString:@"第三球五码"]){
            if (openArr.count>2) {
                NSString *str=openArr[2];
                for (NSString *strNum in arrNum) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.PlayName isEqualToString:@"第四球五码"]){
            if (openArr.count>3) {
                NSString *str=openArr[3];
                for (NSString *strNum in arrNum) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.PlayName isEqualToString:@"第五球五码"]){
            if (openArr.count>4) {
                NSString *str=openArr[4];
                for (NSString *strNum in arrNum) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.PlayName isEqualToString:@"第六球五码"]){
            if (openArr.count>5) {
                NSString *str=openArr[5];
                for (NSString *strNum in arrNum) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.PlayName isEqualToString:@"第七球五码"]){
            if (openArr.count>6) {
                NSString *str=openArr[6];
                for (NSString *strNum in arrNum) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.PlayName isEqualToString:@"胆一码"]||[model.PlayName isEqualToString:@"胆二码"]||[model.PlayName isEqualToString:@"胆三码"]||[model.PlayName isEqualToString:@"18码"]||[model.PlayName isEqualToString:@"12码"]){
            for (NSString *str in arrNum) {
                for (NSString *strNum in openArr) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.PlayName isEqualToString:@"独胆"]){
            for (NSString *str in arrNum) {
                for (NSString *strNum in openArr) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }
    }else if (type==LotteryNumTypeHappy10){
        if ([model.PlayName isEqualToString:@"第一球十码"]) {
            if (openArr.count>0) {
                NSString *str=openArr[0];
                for (NSString *strNum in arrNum) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.PlayName isEqualToString:@"第二球十码"]){
            if (openArr.count>1) {
                NSString *str=openArr[1];
                for (NSString *strNum in arrNum) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.PlayName isEqualToString:@"第三球十码"]){
            if (openArr.count>2) {
                NSString *str=openArr[2];
                for (NSString *strNum in arrNum) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.PlayName isEqualToString:@"第四球十码"]){
            if (openArr.count>3) {
                NSString *str=openArr[3];
                for (NSString *strNum in arrNum) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.PlayName isEqualToString:@"第五球十码"]){
            if (openArr.count>4) {
                NSString *str=openArr[4];
                for (NSString *strNum in arrNum) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.PlayName isEqualToString:@"第六球十码"]){
            if (openArr.count>5) {
                NSString *str=openArr[5];
                for (NSString *strNum in arrNum) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.PlayName isEqualToString:@"第七球十码"]){
            if (openArr.count>6) {
                NSString *str=openArr[6];
                for (NSString *strNum in arrNum) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.PlayName isEqualToString:@"第八球十码"]){
            if (openArr.count>7) {
                NSString *str=openArr[7];
                for (NSString *strNum in arrNum) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.PlayName isEqualToString:@"独胆"]){
            
            for (NSString *str in arrNum) {
                for (NSString *strNum in openArr) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
            
        }
        return attStr;
        
    }else if (type==LotteryNumTypeNearlyThree){
        if ([model.PlayName isEqualToString:@"独胆"]||[model.PlayName isEqualToString:@"双胆"]) {
            for (NSString *str in arrNum) {
                for (NSString *strNum in openArr) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.PlayName isEqualToString:@"和值8码"]){
            NSInteger sum=0;
            for (NSString *strNum in openArr) {
                sum+=[strNum integerValue];
            }
            
            NSArray  *array = [model.BetNum componentsSeparatedByString:@","];
            NSString *sumStr = [NSString stringWithFormat:@"%zd",sum];
            //MARK:元素下标
            NSInteger index = [array indexOfObject:sumStr];
          
            NSInteger frontStrLength = 0;
            for (NSInteger i= 0; i<index; i++) {
                
                frontStrLength += [array[i] length];
                
            }

            NSRange rang = NSMakeRange(frontStrLength+index,sumStr.length);
//            NSRange rang=[model.BetNum rangeOfString:[NSString stringWithFormat:@"%ld",sum]];
            [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
            return attStr;
        }else if ([model.BigPlayName isEqualToString:@"第一球"]){
            if (openArr.count>0) {
                NSString *strNum=openArr[0];
                NSRange rang=[model.BetNum rangeOfString:strNum];
                [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                return attStr;
            }
        }else if ([model.BigPlayName isEqualToString:@"第二球"]){
            if (openArr.count>1) {
                NSString *strNum=openArr[1];
                NSRange rang=[model.BetNum rangeOfString:strNum];
                [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                return attStr;
            }
        }else if ([model.BigPlayName isEqualToString:@"第三球"]){
            if (openArr.count>2) {
                NSString *strNum=openArr[2];
                NSRange rang=[model.BetNum rangeOfString:strNum];
                [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                return attStr;
            }
        }
    }else if (type==LotteryNumTypeLottery){
        if ([model.PlayName isEqualToString:@"十码中特"]||[model.PlayName isEqualToString:@"二十码中特"]) {
            for (NSString *str in arrNum) {
                NSString *strNum=openArr.lastObject;
                if ([str isEqualToString:strNum]) {
                    NSRange rang=[model.BetNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    break;
                }
            }
        }else if ([model.PlayName isEqualToString:@"平五码"]){
            for (NSString *str in arrNum) {
                for (int i=0;i<openArr.count-1;i++) {
                    NSString *strNum=openArr[i];
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.PlayName isEqualToString:@"六肖中特"]){
            for (NSString *str in arrNum) {
                NSString *lastNum=[TGTools getShengXiao:openArr.lastObject];
                if ([str isEqualToString:lastNum]) {
                    NSRange rang=[model.BetNum rangeOfString:str];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    break;
                }
            }
        }else if ([model.PlayName isEqualToString:@"特码波色"]){
            
            [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:NSMakeRange(0, 2)];
            
        }
    }else if (type==LotteryNumTypeChromosphere){
        if ([model.PlayName isEqualToString:@"红3胆"]||[model.PlayName isEqualToString:@"红2胆"]||[model.PlayName isEqualToString:@"红18码"]) {
            for (NSString *str in arrNum) {
                for (int i=0;i<openArr.count-1;i++) {
                    NSString *strNum=openArr[i];
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.BetNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.PlayName isEqualToString:@"蓝8码"]||[model.PlayName isEqualToString:@"蓝5码"]){
            for (NSString *str in arrNum) {
                NSString *strNum=openArr.lastObject;
                if ([str isEqualToString:strNum]) {
                    NSRange rang=[model.BetNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    break;
                }
            }
        }
    }else if (type==LotteryNumTypeHappy12){
        NSInteger index;
        if ([model.PlayName isEqualToString:@"第一球六码"]){
            index=0;
        }else if ([model.PlayName isEqualToString:@"第二球六码"]){
            index=1;
        }else if ([model.PlayName isEqualToString:@"第三球六码"]){
            index=2;
        }else if ([model.PlayName isEqualToString:@"第四球六码"]){
            index=3;
        }else if ([model.PlayName isEqualToString:@"第五球六码"]){
            index=4;
        }else{
            index=100;
        }
        
        if (openArr.count>index) {
            NSString *str=openArr[index];
            for (NSString *strNum in arrNum) {
                if ([str isEqualToString:strNum]) {
                    NSRange rang=[model.BetNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    break;
                }
            }
        }
    }
    return attStr;
}

- (NSMutableAttributedString *)attStrWithOpenNum:(BetModel *)model{
    NSMutableAttributedString *attStr=[[NSMutableAttributedString alloc]initWithString:model.CorrectNum];
    NSArray *arrNum=[model.BetNum componentsSeparatedByString:@","];
    NSArray *openArr=[model.CorrectNum componentsSeparatedByString:@","];
    LotteryNumType type=[[BaseVM manage] getType:model.lType];
    if (type==LotteryNumTypeCarPK10||type==LotteryNumTypeLuckyAirship){
        NSInteger index=0;
        if ([model.BigPlayName isEqualToString:@"冠军"]) {
            index=0;
        }else if ([model.BigPlayName isEqualToString:@"亚军"]){
            index=1;
        }else if ([model.BigPlayName isEqualToString:@"第三名"]){
            index=2;
        }else if ([model.BigPlayName isEqualToString:@"第四名"]){
            index=3;
        }else if ([model.BigPlayName isEqualToString:@"第五名"]){
            index=4;
        }else if ([model.BigPlayName isEqualToString:@"第六名"]){
            index=5;
        }else if ([model.BigPlayName isEqualToString:@"第七名"]){
            index=6;
        }else if ([model.BigPlayName isEqualToString:@"第八名"]){
            index=7;
        }else if ([model.BigPlayName isEqualToString:@"第九名"]){
            index=8;
        }else if ([model.BigPlayName isEqualToString:@"第十名"]){
            index=9;
        }
        if (openArr.count>index) {
            NSString *strNum=openArr[index];
            NSRange rang=[model.CorrectNum rangeOfString:strNum];
            if (rang.location+rang.length<index*2) {
                NSString *str=[model.CorrectNum substringFromIndex:rang.location+rang.length];
                NSRange newRang=[str rangeOfString:strNum];
                newRang=NSMakeRange(rang.location+rang.length+newRang.location, rang.length);
                [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:newRang];
            }else{
                [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
            }
            
            return attStr;
        }
        
        
    }else if (type==LotteryNumTypeBrings) {
        if ([model.PlayName isEqualToString:@"前区4码"]||[model.PlayName isEqualToString:@"前区18码"]) {
            for (NSString *str in arrNum) {
                for (int i=0;i<openArr.count-2;i++) {
                    NSString *strNum=openArr[i];
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.CorrectNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.PlayName isEqualToString:@"后区3码"]||[model.PlayName isEqualToString:@"后区6码"]){
            if (openArr.count!=7) {
                return attStr;
            }
            for (NSString *str in arrNum) {
                for (int i=5;i<openArr.count;i++) {
                    NSString *strNum=openArr[i];
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.CorrectNum rangeOfString:strNum];
                        if (rang.location<model.CorrectNum.length-5) {
                            NSString *str=[model.CorrectNum substringFromIndex:rang.location+rang.length];
                            NSRange newRang=[str rangeOfString:strNum];
                            newRang=NSMakeRange(rang.location+rang.length+newRang.location, rang.length);
                            [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:newRang];
                        }else{
                            [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        }
                        break;
                    }
                }
            }
        }
    }else if (type==LotteryNumTypeLottery3D||type==LotteryNumTypeArrangement3||type==LotteryNumTypeArrangement5||type==LotteryNumTypeSevenLecai||type==LotteryNumTypeSevenStars||type==LotteryNumTypeAllTime) {
        if ([model.PlayName isEqualToString:@"胆一码"]||[model.PlayName isEqualToString:@"胆二码"]||[model.PlayName isEqualToString:@"胆三码"]||[model.PlayName isEqualToString:@"18码"]||[model.PlayName isEqualToString:@"12码"]){
            for (NSString *str in arrNum) {
                for (NSString *strNum in openArr) {
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.CorrectNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.BigPlayName isEqualToString:@"第一球"]){
            if (openArr.count>0) {
                NSString *strNum=openArr[0];
                NSRange rang=[model.CorrectNum rangeOfString:strNum];
                [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                return attStr;
            }
        }else if ([model.BigPlayName isEqualToString:@"第二球"]){
            if ([model.WinState intValue]==3) {
                if (openArr.count>1) {
//                    NSString *strNum=openArr[1];
//                    NSRange rang=[model.CorrectNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:NSMakeRange(2, 1)];
                    return attStr;
                }
            }
            
        }else if ([model.BigPlayName isEqualToString:@"第三球"]){
            if ([model.WinState intValue]==3) {
                if (openArr.count>2) {
//                    NSString *strNum=openArr[2];
//                    NSRange rang=[model.CorrectNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:NSMakeRange(4, 1)];
                    return attStr;
                }
            }
            
        }else if ([model.BigPlayName isEqualToString:@"第四球"]){
            if ([model.WinState intValue]==3) {
                if (openArr.count>3) {
//                    NSString *strNum=openArr[3];
//                    NSRange rang=[model.CorrectNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:NSMakeRange(6, 1)];
                    return attStr;
                }
            }
        }else if ([model.BigPlayName isEqualToString:@"第五球"]){
            if ([model.WinState intValue]==3) {
                if (openArr.count>4) {
//                    NSString *strNum=openArr[4];
//                    NSRange rang=[model.CorrectNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:NSMakeRange(8, 1)];
                    return attStr;
                }
            }
            
        }else if ([model.BigPlayName isEqualToString:@"第六球"]){
            if ([model.WinState intValue]==3) {
                if (openArr.count>5) {
//                    NSString *strNum=openArr[5];
//                    NSRange rang=[model.CorrectNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:NSMakeRange(10, 1)];
                    return attStr;
                }
            }
            
        }else if ([model.BigPlayName isEqualToString:@"第七球"]){
            if ([model.WinState intValue]==3) {
                if (openArr.count>6) {
//                    NSString *strNum=openArr[6];
//                    NSRange rang=[model.CorrectNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:NSMakeRange(12, 1)];
                    return attStr;
                }
            }
        }else if ([model.BigPlayName isEqualToString:@"独胆"]){
            
            for (NSString *str in arrNum) {
                for (NSString *strNum in openArr) {
                    if ([str isEqualToString:strNum]) {
                        NSArray *rangArr = [self rangeOfSubString:str inString:model.CorrectNum];
                        for ( NSValue *value in rangArr) {
                            NSRange range = [value rangeValue];
                            [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:range];
                        }
                        break;
                    }
                }
            }
        
        }
    }else if (type==LotteryNumTypeHappy10||type==LotteryNumType11Select5){
        if ([model.BigPlayName isEqualToString:@"第一球"]){
            if (openArr.count>0) {
                NSString *strNum=openArr[0];
                NSRange rang=[model.CorrectNum rangeOfString:strNum];
                [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                return attStr;
            }
        }else if ([model.BigPlayName isEqualToString:@"第二球"]){
            if ([model.WinState intValue]==3) {
                if (openArr.count>1) {
                    NSString *strNum=openArr[1];
                    NSRange rang=[model.CorrectNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    return attStr;
                }
            }
            
        }else if ([model.BigPlayName isEqualToString:@"第三球"]){
            if ([model.WinState intValue]==3) {
                if (openArr.count>2) {
                    NSString *strNum=openArr[2];
                    NSRange rang=[model.CorrectNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    return attStr;
                }
            }
            
        }else if ([model.BigPlayName isEqualToString:@"第四球"]){
            if ([model.WinState intValue]==3) {
                if (openArr.count>3) {
                    NSString *strNum=openArr[3];
                    NSRange rang=[model.CorrectNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    return attStr;
                }
            }
        }else if ([model.BigPlayName isEqualToString:@"第五球"]){
            if ([model.WinState intValue]==3) {
                if (openArr.count>4) {
                    NSString *strNum=openArr[4];
                    NSRange rang=[model.CorrectNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    return attStr;
                }
            }
            
        }else if ([model.BigPlayName isEqualToString:@"第六球"]){
            if ([model.WinState intValue]==3) {
                if (openArr.count>5) {
                    NSString *strNum=openArr[5];
                    NSRange rang=[model.CorrectNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    return attStr;
                }
            }
            
        }else if ([model.BigPlayName isEqualToString:@"第七球"]){
            if ([model.WinState intValue]==3) {
                if (openArr.count>6) {
                    NSString *strNum=openArr[6];
                    NSRange rang=[model.CorrectNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    return attStr;
                }
            }
            
        }else if ([model.BigPlayName isEqualToString:@"第八球"]){
            if ([model.WinState intValue]==3) {
                if (openArr.count>7) {
                    NSString *strNum=openArr[7];
                    NSRange rang=[model.CorrectNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    return attStr;
                }
            }
            
        }else if ([model.BigPlayName isEqualToString:@"独胆"]){
            
                    for (NSString *str in arrNum) {
                        for (NSString *strNum in openArr) {
                            if ([str isEqualToString:strNum]) {
                                NSRange rang=[model.CorrectNum rangeOfString:strNum];
                                [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                                break;
                            }
                        }
            }
        }
        
    }else if (type==LotteryNumTypeNearlyThree ){
        if ([model.PlayName isEqualToString:@"独胆"]||[model.PlayName isEqualToString:@"双胆"]) {
            
            for (NSString *str in arrNum) {
                for (NSString *strNum in openArr) {
                    if ([str isEqualToString:strNum]) {
                        NSArray *rangArr = [self rangeOfSubString:str inString:model.CorrectNum];
                        
                        NSLog(@"%@--%@",rangArr,rangArr[0]);
                        for ( NSValue *value in rangArr) {
                            NSRange range = [value rangeValue];
                            [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:range];
                        }
                        
                        break;
                    }
                }
            }
        }else if ([model.BigPlayName isEqualToString:@"第一球"]){
            if ([model.WinState intValue]==3) {
                if (openArr.count>0) {
                    NSString *strNum=openArr[0];
                    NSRange rang=[model.CorrectNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    return attStr;
                }
            }
        }else if ([model.BigPlayName isEqualToString:@"第二球"]){
            if ([model.WinState intValue]==3) {
                if (openArr.count>1) {
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:NSMakeRange(2, 1)];
                    return attStr;
                }
            }
        }else if ([model.BigPlayName isEqualToString:@"第三球"]){
            if ([model.WinState intValue]==3) {
                if (openArr.count>2) {
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:NSMakeRange(attStr.length-1, 1)];
                    return attStr;
                }
            }
        }
    }else if (type==LotteryNumTypeLottery){
        if ([model.PlayName isEqualToString:@"十码中特"]||[model.PlayName isEqualToString:@"二十码中特"]) {
            for (NSString *str in arrNum) {
                NSString *strNum=openArr.lastObject;
                if ([str isEqualToString:strNum]) {
                    NSRange rang=[model.CorrectNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    break;
                }
            }
        }else if ([model.PlayName isEqualToString:@"平五码"]){
            for (NSString *str in arrNum) {
                for (int i=0;i<openArr.count-1;i++) {
                    NSString *strNum=openArr[i];
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.CorrectNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.PlayName isEqualToString:@"六肖中特"]){
            NSString *lastNum=openArr.lastObject;
            NSRange rang=[model.CorrectNum rangeOfString:lastNum];
            [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
            return attStr;
        }else if ([model.PlayName isEqualToString:@"平特一肖"]){
            NSString *shenxgiao=model.BetNum;
            for (NSString *str in openArr) {
                NSString *strShengxiao=[TGTools getShengXiao:str];
                if ([strShengxiao isEqualToString:shenxgiao]) {
                    NSRange rang=[model.CorrectNum rangeOfString:str];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                }
            }
            //MARK:新增
        }else if ([model.PlayName isEqualToString:@"特码单双"]||[model.PlayName isEqualToString:@"特码大小"]||[model.PlayName isEqualToString:@"特码波色"]){
            

            [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:NSMakeRange(attStr.length-2, 2)];

            
        }
    }else if (type==LotteryNumTypeChromosphere){
        if ([model.PlayName isEqualToString:@"红3胆"]||[model.PlayName isEqualToString:@"红2胆"]||[model.PlayName isEqualToString:@"红18码"]) {
            for (NSString *str in arrNum) {
                for (int i=0;i<openArr.count-1;i++) {
                    NSString *strNum=openArr[i];
                    if ([str isEqualToString:strNum]) {
                        NSRange rang=[model.CorrectNum rangeOfString:strNum];
                        [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                        break;
                    }
                }
            }
        }else if ([model.PlayName isEqualToString:@"蓝8码"]||[model.PlayName isEqualToString:@"蓝5码"]){
            for (NSString *str in arrNum) {
                NSString *strNum=openArr.lastObject;
                if ([str isEqualToString:strNum]) {
                    NSRange rang=[model.CorrectNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    break;
                }
            }
        }
    }else if (type==LotteryNumTypeHappy12){
        NSInteger index;
        if ([model.PlayName isEqualToString:@"第一球六码"]){
            index=0;
        }else if ([model.PlayName isEqualToString:@"第二球六码"]){
            index=1;
        }else if ([model.PlayName isEqualToString:@"第三球六码"]){
            index=2;
        }else if ([model.PlayName isEqualToString:@"第四球六码"]){
            index=3;
        }else if ([model.PlayName isEqualToString:@"第五球六码"]){
            index=4;
        }else{
            index=100;
        }
        
        if (openArr.count>index) {
            NSString *str=openArr[index];
            for (NSString *strNum in arrNum) {
                if ([str isEqualToString:strNum]) {
                    NSRange rang=[model.CorrectNum rangeOfString:strNum];
                    [attStr addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:rang];
                    break;
                }
            }
        }
    }
    return attStr;
}


#pragma mark - 获取这个字符串ASting中的所有abc的所在的index
- (NSArray*)rangeOfSubString:(NSString*)subStr inString:(NSString*)string {
    
          NSMutableArray *rangeArray = [NSMutableArray array];
    
          NSString*string1 = [string stringByAppendingString:subStr];
    
          NSString *temp;
    
          for(int i =0; i < string.length; i ++) {
        
                    temp = [string1 substringWithRange:NSMakeRange(i, subStr.length)];
        
                    if ([temp isEqualToString:subStr]) {
            
                              NSRange range = {i,subStr.length};
            
                              [rangeArray addObject: [NSValue valueWithRange:range]];
            
                        }
        
              }
    
          return rangeArray;
    
}


@end
